package cn.iocoder.yudao.module.product.spu.vo;

import cn.hutool.core.collection.CollUtil;
import cn.idev.excel.annotation.ExcelProperty;
import cn.iocoder.yudao.framework.business.evaluation.enums.ScoreLevelEnum;
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat;
import cn.iocoder.yudao.framework.excel.core.convert.DictConvert;
import cn.iocoder.yudao.framework.validation.InEnumValue;
import cn.iocoder.yudao.module.api.dine.enums.DineAttributeEnum;
import cn.iocoder.yudao.module.api.product.DictTypeConstants;
import cn.iocoder.yudao.module.api.product.sku.dto.PriceRespDTO;
import cn.iocoder.yudao.module.api.product.spu.dto.ProductSpuForUniAppRespDTO;
import cn.iocoder.yudao.module.api.product.spu.enums.ProductPriceModeEnum;
import cn.iocoder.yudao.module.api.product.spu.enums.ProductSpuRecommendEnum;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.product.sku.vo.AppProductSkuRespVO;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Schema(description = "用户 APP - 产品 SPU Response VO")
@Data
public class AppProductSpuRespVO extends PriceRespDTO {

    @Schema(description = "产品 SPU 信息，用于适配 UNIAPP 的 SKU 组件！！！转换成前端想要的数据格式")
    private ProductSpuForUniAppRespDTO uniapp;

    @Schema(description = "店铺编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long shopId;

    @Schema(description = "店铺信息")
    private ShopInfoDTO shop;

    @Schema(description = "产品类型，可根据主营类目转换得来，如：mall、food、hotel", requiredMode = Schema.RequiredMode.REQUIRED, example = "mall")
    private String type;

    @Schema(description = "产品名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "清凉小短袖")
    private String name;

    @Schema(description = "产品简介", requiredMode = Schema.RequiredMode.REQUIRED, example = "清凉小短袖简介，清凉小短袖简介，清凉小短袖简介")
    private String introduction;

    @Schema(description = "行业类目编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long platformCategoryId;

    @Schema(description = "产品分类编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Long categoryId;

    @Schema(description = "品牌编号", example = "0")
    private Long brandId;

    @Schema(description = "品牌名称", example = "荣耀")
    private String brandName;

    @Schema(description = "产品封面图", requiredMode = Schema.RequiredMode.REQUIRED)
    private String picUrl;

    @Schema(description = "产品轮播图", requiredMode = Schema.RequiredMode.REQUIRED)
    private List<String> sliderPicUrls;

    @SuppressWarnings("unused")
    public List<String> getSliderPicUrls() {
        if (CollUtil.isEmpty(sliderPicUrls)) {
            sliderPicUrls = CollUtil.newArrayList(picUrl);
        }
        return sliderPicUrls;
    }

    @Schema(description = "视频地址")
    private String videoUrl;

    // ========== SKU 相关字段 =========

    @Schema(description = "规格类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
    private Boolean specType;

    @Schema(description = "产品 SKU 数组", requiredMode = Schema.RequiredMode.REQUIRED)
    private List<AppProductSkuRespVO> skus;

    public List<AppProductSkuRespVO> getSkus() {
        if (CollUtil.isEmpty(skus)) {
            skus = new ArrayList<>();
        }
        return skus;
    }

    @Schema(description = "计价模式：1-按单品计价、2-按称量计价")
    @InEnumValue(ProductPriceModeEnum.class)
    private Integer priceMode;

    @Schema(description = "配送类型/服务方式数组：0-无、1-快递发货、2-到店自提、3-同城配送、4-现场核销、5-上门服务", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private List<Integer> deliveryTypes;

    @Schema(description = "成本价，单位使用：分", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
    @JsonIgnore
    private int costPrice; // 不返回成本价给前端用户看到
    @Schema(description = "成本价，单位为元")
    @JsonIgnore
    private String costPriceYuan; // 不返回成本价给前端用户看到

    @Override
    @JsonIgnore // 不返回成本价给前端用户看到
    public int getCostPrice() {
        return super.getCostPrice();
    }

    @JsonIgnore // 不返回成本价给前端用户看到
    public String getCostPriceYuan() {
        if (costPriceYuan == null) {
            return MoneyUtils.fenToYuanStr(getCostPrice());
        }
        return costPriceYuan;
    }

    @Schema(description = "最小销售价，单位使用：元", example = "0.01")
    private String priceMinYuan;
    @Schema(description = "最大销售价，单位使用：元", example = "1.00")
    private String priceMaxYuan;

    @Schema(description = "原价，单位为分")
    private Integer originPrice;

    @SuppressWarnings("unused")
    public Integer getOriginPrice() {
        if (originPrice == null || originPrice <= 0) {
            int price = getMarketPrice();
            if (price <= 0) {
                price = getPrice();
            }
            return price;
        }
        return originPrice;
    }

    @Schema(description = "原价，单位为元")
    private String originPriceYuan;

    @SuppressWarnings("unused")
    public String getOriginPriceYuan() {
        if (originPriceYuan == null) {
            return MoneyUtils.fenToYuanStr(getOriginPrice());
        }
        return originPriceYuan;
    }

    @Schema(description = "库存", requiredMode = Schema.RequiredMode.REQUIRED, example = "666")
    private Integer stock;

    @Schema(description = "产品状态：-1-回收站、0-已下架、1-已上架、2-待审核", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    @ExcelProperty(value = "产品状态", converter = DictConvert.class)
    @DictFormat(DictTypeConstants.PRODUCT_SPU_STATUS)
    private Integer status;

    @Schema(description = "单位名称")
    private String unitName;

    // ========== 营销相关字段 =========

    @Schema(description = "推荐类型：0-无、1-精选、2-爆款、3-新品、4-补贴", example = "0")
    @InEnumValue(ProductSpuRecommendEnum.class)
    private Integer recommend;

    @Schema(description = "是否积分产品", example = "false")
    private Boolean isIntegral;

    @Schema(description = "赠送积分", example = "0")
    @JsonIgnore
    private Integer giveIntegral;

    // ========== 限购相关字段 =========

    @Schema(description = "起购数量/起批数量", example = "1")
    private Integer limitMinQuantity;

    @Schema(description = "限购数量", example = "999")
    private Integer limitMaxQuantity;

    @Schema(description = "每次加购数量", example = "1")
    private Integer stepQuantity;

    @Schema(description = "是否已收藏", example = "false")
    @JsonProperty("favorite")
    private boolean favorite;

    // ========== 统计相关字段 =========

    @Schema(description = "产品总销量", example = "0")
    private Integer salesCount;

    @Schema(description = "产品月销量", example = "0")
    private int salesCountPerMonth;

    @Schema(description = "产品评分", example = "5.0")
    private float score;
    @Schema(description = "评分星级", example = "5")
    private Integer scoreLevel;

    @SuppressWarnings("unused")
    public Integer getScoreLevel() {
        return ScoreLevelEnum.fromScore(score).getValue();
    }

    @Schema(description = "是否有优惠券", example = "false")
    private boolean hasCoupon;

    @Schema(description = "标签，英文逗号分隔")
    private List<String> labels;

    @Schema(description = "菜肴属性：0-无、1-主食、2-荤菜、3-素菜、4-汤、5-调料、6-饮品、7-水果、20-锅底、50-套餐、98-餐盒、99-备注")
    @InEnumValue(DineAttributeEnum.class)
    private Integer dishAttribute;
    @Schema(description = "菜肴属性：0-无、1-主食、2-荤菜、3-素菜、4-汤、5-调料、6-饮品、7-水果、20-锅底、50-套餐、98-餐盒、99-备注")
    private String dishAttributeName;

    @SuppressWarnings("unused")
    public String getDishAttributeName() {
        if (dishAttributeName == null) {
            return DineAttributeEnum.getByValue(getDishAttribute()).getName();
        }
        return dishAttributeName;
    }

}
